package com.cskaoyan.mall.service;

import com.cskaoyan.mall.bean.MapBean;
import com.cskaoyan.mall.bean.bo.ExpressConfigBO;
import com.cskaoyan.mall.bean.bo.MallConfigBO;
import com.cskaoyan.mall.bean.bo.OrderConfigBO;
import com.cskaoyan.mall.bean.bo.WxConfigBO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cskaoyan.mall.mapper.ConfigurationMapper;
import com.cskaoyan.mall.bean.Configuration;
import com.cskaoyan.mall.service.ConfigurationService;
import sun.util.locale.provider.FallbackLocaleProviderAdapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConfigurationServiceImpl implements ConfigurationService{

    @Resource
    private ConfigurationMapper configurationMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return configurationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Configuration record) {
        return configurationMapper.insert(record);
    }

    @Override
    public int insertSelective(Configuration record) {
        return configurationMapper.insertSelective(record);
    }

    @Override
    public Configuration selectByPrimaryKey(Integer id) {
        return configurationMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Configuration record) {
        return configurationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Configuration record) {
        return configurationMapper.updateByPrimaryKey(record);
    }

    @Override
    public Map<String, String> getInfoByConfigClass(String configName) {
        List<MapBean> list = configurationMapper.getInfoByConfigClass(configName);
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        for (MapBean bean : list) {
            map.put((String)bean.getKey(),(String)bean.getValue());
        }
        return map;
    }

    @Override
    public boolean updateMallInfo(MallConfigBO config) {
        String address = config.getCskaoyanmall_mall_address();
        String name = config.getCskaoyanmall_mall_name();
        String phone = config.getCskaoyanmall_mall_phone();
        String qq = config.getCskaoyanmall_mall_qq();
        ArrayList<MapBean> list = new ArrayList<>();
        list.add(new MapBean("cskaoyanmall_mall_address", address));
        list.add(new MapBean("cskaoyanmall_mall_name", name));
        list.add(new MapBean("cskaoyanmall_mall_phone", phone));
        list.add(new MapBean("cskaoyanmall_mall_qq", qq));

        return updateConfigInfo(list);
    }

    @Override
    public boolean updateExpressInfo(ExpressConfigBO config) {
        String freight_min = config.getCskaoyanmall_express_freight_min();
        String freight_value = config.getCskaoyanmall_express_freight_value();
        ArrayList<MapBean> list = new ArrayList<>();
        list.add(new MapBean("cskaoyanmall_express_freight_min", freight_min));
        list.add(new MapBean("cskaoyanmall_express_freight_value", freight_value));
        return updateConfigInfo(list);
    }

    @Override
    public boolean updateOrderInfo(OrderConfigBO config) {
        String comment = config.getCskaoyanmall_order_comment();
        String unconfirm = config.getCskaoyanmall_order_unconfirm();
        String unpaid = config.getCskaoyanmall_order_unpaid();
        ArrayList<MapBean> list = new ArrayList<>();
        list.add(new MapBean("cskaoyanmall_order_comment", comment));
        list.add(new MapBean("cskaoyanmall_order_unconfirm", unconfirm));
        list.add(new MapBean("cskaoyanmall_order_unpaid", unpaid));
        return updateConfigInfo(list);
    }

    @Override
    public boolean updateWXInfo(WxConfigBO config) {
        String catlogGoods = config.getCskaoyanmall_wx_catlog_goods();
        String catlogList = config.getCskaoyanmall_wx_catlog_list();
        String indexBrand = config.getCskaoyanmall_wx_index_brand();
        String indexHot = config.getCskaoyanmall_wx_index_hot();
        String indexNew = config.getCskaoyanmall_wx_index_new();
        String indexTopic = config.getCskaoyanmall_wx_index_topic();
        String share = config.getCskaoyanmall_wx_share();
        ArrayList<MapBean> list = new ArrayList<>();
        list.add(new MapBean("cskaoyanmall_wx_catlog_goods", catlogGoods));
        list.add(new MapBean("cskaoyanmall_wx_catlog_list", catlogList));
        list.add(new MapBean("cskaoyanmall_wx_index_brand", indexBrand));
        list.add(new MapBean("cskaoyanmall_wx_index_hot", indexHot));
        list.add(new MapBean("cskaoyanmall_wx_index_new", indexNew));
        list.add(new MapBean("cskaoyanmall_wx_index_topic", indexTopic));
        list.add(new MapBean("cskaoyanmall_wx_share", share));
        return updateConfigInfo(list);
    }

    protected boolean updateConfigInfo(List<MapBean> list){
        int num = 0;
        for (MapBean bean : list) {
            num += configurationMapper.updateConfigInfo(bean);
        }

        return list.size()==num ? true : false;
    }

}
